<?php
class MAmazonStaticOrder extends MY_Model
{

    protected $table = 'amazon_static_order';
    const STATE_UNABLE = 0;
    const STATE_ABLE = 1;

    function __construct()
    {
        parent::__construct();
    }

    public function getById($id)
    {
        $query = $this->db->where('id', $id)->get($this->table);
        return $query->row();
    }

    public function getAllStatic()
    {

        $queryResult = $this->getAll();
        $kvArr = array();
        foreach ($queryResult as $k => $v) {
            $kvArr[$v->id] = $v->name;
        }
        return $kvArr;
    }

    public function deleteByStaticId($static_id, $shop_id){
        $sql = "delete from core_amazon_static_order where static_id = ".$static_id ." and shop_id = ".$shop_id;
        return $this->db->query($sql);
    }

    public static function getState($state = NULL)
    {
        $define = array(self::STATE_ABLE => '<span class="label label-success">可用</span>', self::STATE_UNABLE => '<span class="label label-danger">禁用</span>',);
        if ($state === NULL) {
            return $define;
        } else {
            return key_exists($state, $define) ? $define[$state] : '';
        }
    }

    public function getList($limit, $sort = false)
    {
        $this->db->select('*');
        $this->db->distinct();
        $this->db->from($this->table);

        $this->formfilter->doFilter();
        $this->db->limit($limit['limit'], $limit['offset']);
        $this->db->order_by('id', 'desc');
        $query = $this->db->get();
        return $query->result();
    }


    public function getCount()
    {
        $this->db->select('count(distinct id) as num');
        $this->db->from($this->table);

        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getQuery($limit = null)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($this->table . '.del', UNDELETED);
        if ($limit != null)
            $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();
        $rtn = $this->db->get();
        return $rtn;
    }

}
